package com.zcoj.codeSimilar.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zcoj.codeSimilar.mapper.CodeSimilarMapper;
import com.zcoj.codeSimilar.pojo.DTO.CodeSimilar;
import com.zcoj.codeSimilar.pojo.VO.CodeSimilarVo;
import com.zcoj.codeSimilar.service.CodeSimilarService;
import com.zcoj.pojo.Dto.ContestProblem;
import com.zcoj.service.ContestProblemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author chenbin
 * @version 1.0
 * @description: TODO
 * @date 2023/8/5 14:24
 */
@Service
public class CodeSimilarServiceImpl implements CodeSimilarService {

	@Autowired
	private CodeSimilarMapper codeSimilarMapper;

	@Autowired
	private ContestProblemService contestProblemService;

	@Override
	public void insertCodeSimilar(CodeSimilar codeSimilar) {
		codeSimilarMapper.insert(codeSimilar);
	}

	@Override
	public List<CodeSimilarVo> searchContestCodeSimilarity(Integer cid) {
		CodeSimilar codeSimilar = new CodeSimilar();
		codeSimilar.setCid(cid);
		QueryWrapper<CodeSimilar> codeSimilarQueryWrapper=new QueryWrapper<>(codeSimilar);
		List<CodeSimilar> codeSimilarList = codeSimilarMapper.selectList(codeSimilarQueryWrapper);
		List<ContestProblem> contestProblemServiceList = contestProblemService.findList(new ContestProblem().setCid(cid));
		List<CodeSimilarVo> codeSimilarVos=new ArrayList<>();
		if(contestProblemServiceList.size()>0){
			for (ContestProblem contestProblem : contestProblemServiceList) {
				CodeSimilarVo codeSimilarVo = new CodeSimilarVo();
				codeSimilarVo.setPid(contestProblem.getPid());
				codeSimilarVo.setPName(contestProblem.getDisplayTitle());
				List<CodeSimilar> codeSimilarLists=new ArrayList<>();
				for (CodeSimilar similar : codeSimilarList) {
					if(similar.getPid().equals(codeSimilarVo.getPid())){
						codeSimilarLists.add(similar);
					}
				}
				codeSimilarVo.setCodeSimilars(codeSimilarLists);
				codeSimilarVos.add(codeSimilarVo);
			}
			return codeSimilarVos;

		}
		return null;

	}

	@Override
	public void deleteCodeSimilarRecord(Integer cid) {
		CodeSimilar codeSimilar = new CodeSimilar();
		codeSimilar.setCid(cid);
		QueryWrapper<CodeSimilar> codeSimilarQueryWrapper=new QueryWrapper<>(codeSimilar);
		codeSimilarMapper.delete(codeSimilarQueryWrapper);
	}
}
